import duckdb, os
from utils.path_utils import get_project_root
from database.duckdb.duckdb_base import get_connection


# 创建 NW_PROJECT_HSM 表
def create_table():
    con = get_connection()
    con.execute("""
        CREATE TABLE IF NOT EXISTS NW_PROJECT_HSM (
          uuid VARCHAR(32) PRIMARY KEY,
          dwbh VARCHAR(100),
          project_uuid VARCHAR(32),
          kjnd VARCHAR(10),
          hsm_xmbh VARCHAR(500),
          hsm_xmnm VARCHAR(500),
          hsm_name VARCHAR(500),
          hsm_fullname VARCHAR(2000),
          hsm_xmlb VARCHAR(200),
          calc VARCHAR(100),
          qzq_type VARCHAR(20) DEFAULT 'KY',
          model VARCHAR(200),
          status CHAR(1) DEFAULT '1',
          create_by VARCHAR(200),
          create_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
          remarks VARCHAR(500)
        )
    """)
    con.close()

# 获取所有记录
def get_all_data():
    con = get_connection()
    query = "SELECT * FROM NW_PROJECT_HSM"
    result = con.execute(query).fetchall()
    con.close()
    return result

# 按条件获取记录
def get_data_by_condition(dwbh, model, qzq_type):
    con = get_connection()
    result = con.execute("SELECT * FROM NW_PROJECT_HSM "
                         " WHERE dwbh = ? and model = ? and  qzq_type = ?"
                         "   and status = '1'",
                         (dwbh, model, qzq_type)).fetchall()
    con.close()
    return result


# 按条件获取记录
def get_data_by_condition(dwbh, model, qzq_type, project_uuid):
    con = get_connection()
    result = con.execute("SELECT * FROM NW_PROJECT_HSM "
                         " WHERE dwbh = ? and model = ? and  qzq_type = ? and project_uuid = ? "
                         "   and status = '1'",
                         (dwbh, model, qzq_type, project_uuid)).fetchall()
    con.close()
    return result


# 根据 ID 获取单条记录
def get_data_by_uuid(uuid):
    con = get_connection()
    query = "SELECT * FROM NW_PROJECT_HSM WHERE uuid = ?"
    result = con.execute(query, (uuid,)).fetchone()
    con.close()
    return result

# 添加记录
def add_data(uuid, dwbh, model, qzq_type, project_uuid, kjnd, hsm_xmbh, hsm_xmnm, hsm_name, hsm_fullname, hsm_xmlb, calc, status, create_by, remarks):
    con = get_connection()
    con.execute("""
        INSERT INTO NW_PROJECT_HSM (uuid, dwbh, model, qzq_type, project_uuid, kjnd, hsm_xmbh, hsm_xmnm, hsm_name, hsm_fullname, hsm_xmlb, calc, status, create_by, remarks)
        VALUES ( ?, ?, ?, ?, ?, ?, ?,  ?, ?, ?, ?, ?, ?, ?, ?)
    """, (uuid, dwbh, model, qzq_type, project_uuid, kjnd, hsm_xmbh, hsm_xmnm, hsm_name, hsm_fullname, hsm_xmlb, calc, status, create_by, remarks))
    con.close()

# 更新记录
def update_data(uuid, dwbh, model, qzq_type, project_uuid, kjnd, hsm_xmbh, hsm_xmnm, hsm_name, hsm_fullname, hsm_xmlb, calc, status, create_by, remarks):
    con = get_connection()
    con.execute("""
        UPDATE NW_PROJECT_HSM
        SET dwbh = ?, model = ?, qzq_type = ?, project_uuid = ?, kjnd = ?, hsm_xmbh = ?, hsm_xmnm = ?, hsm_name = ?,
         hsm_fullname = ?, hsm_xmlb = ?, calc = ?,
         status = ?, create_by = ?, remarks = ?
        WHERE uuid = ?
    """, ( dwbh, model, qzq_type, project_uuid, kjnd, hsm_xmbh, hsm_xmnm, hsm_name, hsm_fullname, hsm_xmlb, calc, status, create_by, remarks, uuid))
    con.close()

# 删除记录
def delete_data(uuid):
    con = get_connection()
    con.execute("DELETE FROM NW_PROJECT_HSM WHERE uuid = ?", (uuid,))
    con.close()


